package collectionDemo.linkedListDemo;

import org.junit.Test;
import utils.PrintUtil;

import java.util.*;

/**
 * @author zybw-kf01
 * linkedlist.  Iterator是按照 linkedList.addLast() ,linkedList.add(4);先入顺序。进行遍历的。从队首--》队尾。
 *
 *     //迭代器遍历--正向输出
 * //    ListIterator<Integer> it = list.listIterator();
 * //        while(it.hasNext()){//1：boolean hasNext()判断集合中是否有元素，如果有元素可以迭代，就返回true。
 * //        System.out.print(it.next()+" ");//2： E next()返回迭代的下一个元素
 * //    }
 *     //迭代器遍历--反向输出
 *     private void iterator2(LinkedList<Integer> linkedList) {
 *         if (linkedList != null) {
 *             ListIterator<Integer> iterator = linkedList.listIterator(linkedList.size());
 *             while (iterator.hasPrevious()){
 *                 System.err.println("Demo1:iterator2:"+iterator.previous());
 *             }
 *         }
 *     }
 */
public class Demo1 {
    @Test
    public void test(){


    }

//    public static void main(String[] args) {
    @Test
    public void main() {
        List<String> mList = new LinkedList<>();

        List<Object> synchronizedList = Collections.synchronizedList(new LinkedList<>());

        LinkedList<Integer> linkedList = new LinkedList<>();

//        int poll1 = linkedList.poll();//TODO   null--->int  java.lang.NullPointerException

        linkedList.add(4);
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);

        iterator(linkedList);
        iterator2(linkedList);

        LinkedList<Integer> linked2 = new LinkedList<>();
        linked2.add(4);
        linked2.add(5);
        linked2.add(6);
        linked2.add(7);
        linkedList.addAll(linked2);
        PrintUtil.print(linkedList);

        System.out.println("add 123 is linkLast list== " + linkedList);

        linkedList.addFirst(0);
        System.out.println("addFirst " + 4 + " list== " + linkedList);

        linkedList.addLast(999);
        System.out.println("addLast " + 999 + " list== " + linkedList);

        linkedList.add(2, 222);
        System.out.println("addIndex 2 222 list== " + linkedList);

        linkedList.push(4);
        System.out.println("push is addFirst ==" + linkedList);

        Integer poll = linkedList.poll();
        System.err.println("poll == " + poll + " list==" + linkedList);

        Integer integer = linkedList.get(1);//can find  从近的 头 尾 那一端查找。

        Integer peekFirst = linkedList.peekFirst();
        Integer peekLast = linkedList.peekLast();
        System.err.println("peek== " + linkedList.peek() + " peekFirst== " + peekFirst + " peekLast == " + peekLast + " list==" + linkedList);

        LinkedList<Integer> clone = (LinkedList<Integer>) linkedList.clone();

        linkedList.clear();
        System.out.println("after clear size==" + linkedList.size() + " --clone size== " + clone.size());
        System.out.println("clone list == " + clone);
        System.out.println("when size==0 poll== " + linkedList.poll());

    }

    // * linkedlist.  Iterator是按照 linkedList.addLast() ,linkedList.add(4);先入顺序。进行遍历的。从队首--》队尾。
    private void iterator(LinkedList<Integer> linkedList) {
        if (linkedList != null) {
            Iterator<Integer> iterator = linkedList.iterator();
            while (iterator.hasNext()){
                System.out.println("Demo1:iterator:"+iterator.next());
            }
        }
    }


    //迭代器遍历--正向输出
//    ListIterator<Integer> it = list.listIterator();
//        while(it.hasNext()){//1：boolean hasNext()判断集合中是否有元素，如果有元素可以迭代，就返回true。
//        System.out.print(it.next()+" ");//2： E next()返回迭代的下一个元素
//    }
    //迭代器遍历--反向输出
    private void iterator2(LinkedList<Integer> linkedList) {
        if (linkedList != null) {
            ListIterator<Integer> iterator = linkedList.listIterator(linkedList.size());
            while (iterator.hasPrevious()){
                System.err.println("Demo1:iterator2:"+iterator.previous());
            }
        }
    }
}
